package org.jsola.hr.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.core.query.BaseQuery;
import org.jsola.hr.constant.EmpInfoRequireType;
import org.jsola.hr.constant.EmpInfoScene;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.hr.example.EmpWorkRequireExample.E_COMPANY;
import static org.jsola.hr.example.EmpWorkRequireExample.E_COMPANY_ID;
import static org.jsola.hr.example.EmpWorkRequireExample.E_END_TIME;
import static org.jsola.hr.example.EmpWorkRequireExample.E_LEAVE_REASON;
import static org.jsola.hr.example.EmpWorkRequireExample.E_POSITION;
import static org.jsola.hr.example.EmpWorkRequireExample.E_REQUIRE_TYPE;
import static org.jsola.hr.example.EmpWorkRequireExample.E_START_TIME;
import static org.jsola.hr.example.EmpWorkRequireExample.E_WIT_NAME;
import static org.jsola.hr.example.EmpWorkRequireExample.E_WIT_PHONE;
import static org.jsola.hr.example.EmpWorkRequireExample.N_COMPANY;
import static org.jsola.hr.example.EmpWorkRequireExample.N_COMPANY_ID;
import static org.jsola.hr.example.EmpWorkRequireExample.N_END_TIME;
import static org.jsola.hr.example.EmpWorkRequireExample.N_LEAVE_REASON;
import static org.jsola.hr.example.EmpWorkRequireExample.N_POSITION;
import static org.jsola.hr.example.EmpWorkRequireExample.N_REQUIRE_TYPE;
import static org.jsola.hr.example.EmpWorkRequireExample.N_START_TIME;
import static org.jsola.hr.example.EmpWorkRequireExample.N_WIT_NAME;
import static org.jsola.hr.example.EmpWorkRequireExample.N_WIT_PHONE;

/**
 * @author zhr
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class EmpWorkRequireQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = N_COMPANY_ID, example = E_COMPANY_ID)
    @Length(max = 32)
    private String companyId;

    @ApiParam(value = N_REQUIRE_TYPE, example = E_REQUIRE_TYPE, allowableValues = EmpInfoScene.VALUES_STR)
    @Length(max = 20)
    private String requireType;

    @ApiParam(value = N_COMPANY, example = E_COMPANY, allowableValues = EmpInfoRequireType.VALUES_STR)
    private Integer company;

    @ApiParam(value = N_POSITION, example = E_POSITION, allowableValues = EmpInfoRequireType.VALUES_STR)
    private Integer position;

    @ApiParam(value = N_START_TIME, example = E_START_TIME, allowableValues = EmpInfoRequireType.VALUES_STR)
    private Integer startTime;

    @ApiParam(value = N_END_TIME, example = E_END_TIME, allowableValues = EmpInfoRequireType.VALUES_STR)
    private Integer endTime;

    @ApiParam(value = N_LEAVE_REASON, example = E_LEAVE_REASON, allowableValues = EmpInfoRequireType.VALUES_STR)
    private Integer leaveReason;

    @ApiParam(value = N_WIT_NAME, example = E_WIT_NAME, allowableValues = EmpInfoRequireType.VALUES_STR)
    private Integer witName;

    @ApiParam(value = N_WIT_PHONE, example = E_WIT_PHONE, allowableValues = EmpInfoRequireType.VALUES_STR)
    private Integer witPhone;



    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;



    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public EmpWorkRequireQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

